import View from "./view";
import gameCtrl from "../GameView/gameCtrl";
import gameModel from "../GameView/gameModel";
import gameView from "../GameView/gameView";
import gameEvent from "./gameEvent";

const {ccclass, property} = cc._decorator;

@ccclass
export default class gameEventCtrl extends View {

    private static _gameEventCtrl:gameEventCtrl = null

    private gameCtrl:gameCtrl = null
    private gameModel:gameModel = null
    private gameView:gameView = null


    public static instance():gameEventCtrl{
        if(!this._gameEventCtrl){
            this._gameEventCtrl = new gameEventCtrl()
        }
        return this._gameEventCtrl
    }
    

    public regesterModel(gameCtrl:gameCtrl,gameModel:gameModel,gameView:gameView){
        this.gameCtrl = gameCtrl
        this.gameModel = gameModel
        this.gameView = gameView
    }

    public onEventListen(){
        this.bindEventByView()
        this.bindEventByModel()
        this.bindEventByCtrl()
    }

    public offEventListen(){
        this.unBindEventByView()
        this.unBindEventByModel()
        this.unBindEventByCtrl()
    }

    // 监听视图层事件
    private bindEventByView(){
        let that = this.gameView
        // 监听初始化扑克事件
        this.on(gameEvent.INIT_POKER,that.onEventInit,that)
        // 监听游戏开始事件
        this.on(gameEvent.PLAY,that.onEventPlay,that)
        // 监听扑克动画事件
        this.on(gameEvent.INIT_GROUP,that.onEventInitGroupCard,that)
        //监听扑克移动到收牌区
        this.on(gameEvent.POST_UI_MOVE_RECEICEAREA_FROM_PLAYAREA,that.onEventMovePokerToReceiveArea,that)
        //监听扑克牌翻牌
        this.on(gameEvent.POST_POKER_RESTATUS,that.onEventRestatusPoker,that)

    }
    // 监听数据层事件
    private bindEventByModel(){
         // 监听玩牌区移牌事件
         this.on(gameEvent.POKER_MOVE_PLAYAREA_TO_RECIEVEAREA,this.gameModel.pokerMovePlayAreaToReceiveArea,this.gameModel)


    }
    //监听控制层事件
    private bindEventByCtrl(){
        //监听重新一局事件
        this.on(gameEvent.RESTART,this.gameCtrl.restart,this.gameCtrl)
    }

    private unBindEventByView(){
        this.off(gameEvent.INIT_POKER,this.gameView.onEventInit,this.gameView)
        this.off(gameEvent.PLAY,this.gameView.onEventPlay,this.gameView)
        this.off(gameEvent.INIT_GROUP,this.gameView.onEventInitGroupCard,this.gameView)
        this.off(gameEvent.POST_UI_MOVE_RECEICEAREA_FROM_PLAYAREA,this.gameView.onEventMovePokerToReceiveArea,this.gameView)
        this.off(gameEvent.POST_POKER_RESTATUS,this.gameView.onEventRestatusPoker,this.gameView)


    }

    private unBindEventByModel(){
        this.off(gameEvent.POKER_MOVE_PLAYAREA_TO_RECIEVEAREA,this.gameModel.pokerMovePlayAreaToReceiveArea,this.gameModel)

    }

    private unBindEventByCtrl(){
        this.off(gameEvent.RESTART,this.gameCtrl.restart,this.gameCtrl)

    }
}
